//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "LoginDlg.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmLoginDlg *frmLoginDlg;
//---------------------------------------------------------------------------
__fastcall TfrmLoginDlg::TfrmLoginDlg(TComponent* Owner, TDatabase* database)
        : TForm(Owner)
{
   bridge = new TUserBridge(database);
   attempts = 0;

   user = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TfrmLoginDlg::btnOkClick(TObject *Sender)
{
   user = bridge->GetUser(edtUsername->Text);

   if ((user == NULL) || (user->password.AnsiCompare(edtPassword->Text) != 0))
   {
      ModalResult = mrNone;

      MessageDlg("Username or password is invalid",
                 mtError,
                 TMsgDlgButtons() << mbOK,
                 0);

      edtUsername->SetFocus();

      attempts++;

      if (attempts == 3)
         Close();
   }
}
//---------------------------------------------------------------------------


void __fastcall TfrmLoginDlg::FormDestroy(TObject *Sender)
{
   delete bridge;     
}
//---------------------------------------------------------------------------

